

function autoZTreeHeight(){
    $("#treeDiv").css("height",getWindowHeight()-$('.table_search_div_tree').height() - 9);
    window.onresize = function(){
        $("#treeDiv").css("height",getWindowHeight()-$('.table_search_div_tree').height() - 9);
    }
}
var menu_setting = {
    check:{
        enable:false
//			chkStyle:"checkbox",
//			chkboxType:{"Y":"s","N":"s"}
    },
    data:{
        simpleData:{
            enable:true
        }
    },
    view:{
        showLine:false,
        dblClickExpand:true,
        fontCss:setFontCss_ztree
    },
    callback:{
        onClick:menuzTreeOnClick
    }
};

//添加的菜单权限
var authority_setting = {
    check: {
        enable: false,
    //    chkboxType:  { "Y" : "ps", "N" : "ps" }
    },
    view: {
    	showLine:false,
        dblClickExpand: true,
        fontCss:setFontCss_ztree
    },
    data: {
        simpleData: {
            enable: true
        }
    },
    callback: {
    	//onClick:authorityzTreeOnCheck("authorityAddList","parentId")
    }
};



var zTree,editzTree,addMenuzTree,authorityTree,authorityEditTree;
var DictionaryResult;
var loadDiv = null;
$(function () {
	
	setTimeout(function(){
		$(".grid-list").load(getPath() + "app/dictionary/add/" + 0);
	}, 100);
	
	
	
	loadDiv = $("#loadDiv").loading();//初始化提示操作执行中
	//自动获取树控件高度
	autoZTreeHeight();
	//初始化获取所有菜单
	initGetMenu();
	
	
	
	 $("#refreshCacheBtn").click(function(){
		 createWindow('确定要刷新缓存？',{
             cancleEnable:true,
             callBack:function(){
             	$("#loadDiv").loading().showLoading();
             	 post(getPath() + "app/dictionary/refreshCache",{},function(result){
             		$("#loadDiv").loading().hideLoading();
        			 createWindow("缓存刷新成功").dialog("open");
        		 });
             }
         }).dialog('open');
	 });
	
	/*$("#title").text("数据字典");*/
	 //去除所有控件的虚线框
    removeBlur();
});

//获取所有菜单
function initGetMenu(){	
	post(getPath() + "app/dictionary/tree",{},
	        function(treeNodes){
	            treeNodes[0].open = true;
	            $.fn.zTree.init($("#parentMenuTree"),authority_setting,treeNodes);
	            //左侧的菜单
	            var nodes = deepClone(treeNodes);
	            nodes.splice(0,1);
	            zTree = $.fn.zTree.init($("#menuTree"),menu_setting,nodes);
	        });
}
//删除
function deleteById(){
	var selectedArray = zTree.getSelectedNodes();
	var promptWindow=null;
		if(selectedArray.length == 0){
	        promptWindow = createWindow('请选择需要删除的数据字典'); 
		}else{
			    promptWindow = createWindow('确认删除该数据字典？',{
		            cancleEnable:true,
		            callBack:function(){
		            	var ids = "";
		    			for(var i=0;i<selectedArray.length;i++){
		    				if(ids.length == 0){
		    					ids += selectedArray[i].id;
		    				}else{
		    					ids += ","+selectedArray[i].id;
		    				}
		    			}
		    			loadDiv.showLoading();
		                post(getPath() + "app/dictionary/delete"+"/"+ids ,{},
		                    function(result){
		                	loadDiv.hideLoading();
		                        promptWindow.dialog("close");
		                        var parentMenuTree = $.fn.zTree.getZTreeObj('parentMenuTree');
		                        var menuTree = $.fn.zTree.getZTreeObj('menuTree');
		                        for(var i=0;i<selectedArray.length;i++){
		                            var parentMenuNode = getNodeById('parentMenuTree',selectedArray[i].id);
		                            parentMenuTree.removeNode(parentMenuNode);
		                            menuTree.removeNode(selectedArray[i]);
		                        }
		                        $(".grid-list").html("");
		                        //删除成功，弹出添加成功提示框，当用户点击确定后刷新页面
		                        promptWindow = createWindow("删除成功！");
		                        $(".grid-list").load(getPath() + "app/dictionary/add/" + 0);
		                        /*promptWindow.dialog("open");*/
		                    });
		            }
		        });						
}
		promptWindow.dialog("open");
}

//菜单树的点击事件
function menuzTreeOnClick(event,treeId,treeNode){
	/*$("#title").text("编辑字典");*/
	$(".grid-list").load(getPath() + "app/dictionary/edit/" + treeNode.id);
	
}

/**
 * 获取指定id节点的数据
 * @param treeId 父节点
 * @param id id
 * @returns {*}
 */
function getNodeById(treeId, id){
    var zTree = $.fn.zTree.getZTreeObj(treeId);
    var targetNode = zTree.getNodesByFilter(function (node){
        return node.id == id;
    },true);

    return targetNode;
}

//深度克隆
function deepClone(obj){
    var result,oClass=isClass(obj);
    //确定result的类型
    if(oClass==="Object"){
        result={};
    }else if(oClass==="Array"){
        result=[];
    }else{
        return obj;
    }
    for(key in obj){
        var copy=obj[key];
        if(isClass(copy)=="Object"){
            result[key]=arguments.callee(copy);//递归调用
        }else if(isClass(copy)=="Array"){
            result[key]=arguments.callee(copy);
        }else{
            result[key]=obj[key];
        }
    }
    return result;
}
//返回传递给他的任意对象的类
function isClass(o){
    if(o===null) return "Null";
    if(o===undefined) return "Undefined";
    return Object.prototype.toString.call(o).slice(8,-1);
}